/**
 * 文件：ListEditAdapter.java
 * 包名：com.ronri.list.editview;   --- 正式发布时需要修正
 * 标题：列表编辑组件基类
 * 说明：
 * 作者：MS During
 * 日期：2017-04-10
 * 历史：
 */
package com.ronri.list.editview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import java.util.List;

/***
 * 编辑列表适配器
 */
public class ListEditAdapter
        extends BaseAdapter
        implements AdapterView.OnItemClickListener
{

    private static final String _TAG = "ListEditAdapter";
    private List<AbstractItemElement> _mContentList;
    private LayoutInflater _mInflater;
    private Callback _mCallback;

    private Context _mContext;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(_TAG,"onItemClick");
        _mCallback.click(view);
    }

    /***
     * 说明：定义一个接口，用于回掉ListView中Item的点击事件，这里可以控制Item里面的按钮及其他特殊效果
     * 作者：During
     * 时间：2017-04-10
     */
    public interface Callback {
        public void click(View v);
    }

    /***
     * 构造类
     *
     * @param context     句柄
     * @param contentList 列表
     * @param callback    回调
     */
    public ListEditAdapter(Context context,
                           List<AbstractItemElement> contentList,
                           Callback callback) {
        _mContext = context;
        _mContentList = contentList;
        _mInflater = LayoutInflater.from(context);
        _mCallback = callback;
    }

    /***
     * 获取Item数量
     *
     * @return
     */
    @Override
    public int getCount() {
        Log.i(_TAG, "getCount:" + String.valueOf(_mContentList.size()));
        return _mContentList.size();
    }

    /***
     * 获取Item对象
     *
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        Log.i(_TAG, "getItem:" + String.valueOf(position));
        return _mContentList.get(position);
    }

    /***
     * 获取ItemID
     *
     * @param position
     * @return
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /***
     * 获取ItemView
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position,
                        View convertView,
                        ViewGroup parent) {

        AbstractItemElement element = null;
        if (convertView == null) {
            // 获取当前视图对象
            element = _mContentList.get(position);
            convertView = element.CreateView(parent);

            convertView.setTag(element);
        } else {
            element = (AbstractItemElement) convertView.getTag();
        }

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(_TAG,"onItemClick");

                ((AbstractItemElement)v.getTag()).Excute();
            }
        });

        return convertView;
    }
}
